package pool.api;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created mangcao with 2020/4/3 10:56
 * Description:
 */
public class ScheduledThreadPoolExecutorDe {


    public static void main(String[] args) {

//        test();
        test2();


    }

    private static void test2() {

        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);




        ScheduledFuture<?> scheduledFuture = executor.scheduleWithFixedDelay((Runnable) () -> System.out.println(1), 4, 4, TimeUnit.MICROSECONDS);


        executor.schedule((Runnable) () -> {
            System.out.println("撤销任务");
            scheduledFuture.cancel(false);
        }, 10, TimeUnit.SECONDS);



    }

    private static void test() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        for (int i = 0; i < 30; i++) {
            final int index = i;
            if (i == 0) {
                executor.scheduleAtFixedRate(() ->
                                System.out.println("现在时间 : " + System.currentTimeMillis() + " , 我在运行.")
                        , 0, 2, TimeUnit.SECONDS);
            } else {
                executor.execute(() -> {
                    System.out.println("我不是延迟任务，我优先级最高，我是任务" + index);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    }
}
